package dom4j_xml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import javax.sql.rowset.spi.XmlWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.Scanner;

public class Dom4jparse1 {
    private static Document document;
    public static void save() throws DocumentException {
        SAXReader reader=new SAXReader();
        document=reader.read("E:\\project-java65-parents\\.idea\\Goods.xml");
    }
    public static void showInfo() throws DocumentException {
        save();
        Element root=document.getRootElement();
        Iterator iterator=root.elementIterator();
        while(iterator.hasNext()){
            Element productno=(Element)iterator.next();
            System.out.println(productno.getName()+":"+productno.attributeValue("num")+productno.attribute("price")+productno.attributeValue("discount"));
            Iterator info=productno.elementIterator();
            while (info.hasNext()){
                Element element=(Element) info.next();
                System.out.println(element.getName()+element.getText());
            }
        }
    }
    public static void save1() throws IOException {
        OutputFormat format=OutputFormat.createCompactFormat();
        XMLWriter writer=new XMLWriter(new OutputStreamWriter(new FileOutputStream("E:\\project-java65-parents\\.idea\\Goods.xml")),format);
        writer.write(document);
        writer.close();
    }
    public static void change(String productno,int count) throws DocumentException, IOException {
        save();
        Boolean flag=true;
        Element root=document.getRootElement();
        Iterator iterator=root.elementIterator();
        while(iterator.hasNext()){
            Element productno1=(Element)iterator.next();
            if (productno1.attributeValue("num").equalsIgnoreCase(productno)){
                String socketcount=(Integer.parseInt(productno1.attributeValue("stockcount"))-count)+"";
                productno1.attribute("stockcount").setValue(socketcount);
                flag=false;
            }
        }
        if (!flag==false){
            System.out.println("对不起，没有该商品");
        }
        save1();

    }

    public static void buy(String num,int count) throws DocumentException, IOException {
        save();
        Boolean flag=true;
        Element root=document.getRootElement();
        Iterator iterator=root.elementIterator();
        while(iterator.hasNext()){
            Element productno1=(Element)iterator.next();
            if (productno1.attributeValue("num").equalsIgnoreCase(num)){

                flag=false;
            }
            if (!flag==false){
                System.out.println("对不起，没有该商品");
                break;
            }
            if (count<=Integer.parseInt(productno1.attribute("price").getValue())) {
                Iterator info = productno1.elementIterator();
                while (info.hasNext()) {
                    Element element = (Element) info.next();
                    if (element.getName().equalsIgnoreCase("price")&&productno1.attribute("discount").getValue().equalsIgnoreCase("false")){
                        System.out.println("总价：" + count * Integer.parseInt(element.getText()));
                        String socketcount=(Integer.parseInt(productno1.attributeValue("stockcount"))-count)+"";
                        productno1.attribute("stockcount").setValue(socketcount);

                    }else if (element.getName().equalsIgnoreCase("price")&&productno1.attribute("discount").getValue().equalsIgnoreCase("true")){
                        System.out.println("总价：" + (count * Integer.parseInt(element.getText())*0.7));
                        String socketcount=(Integer.parseInt(productno1.attributeValue("stockcount"))-count)+"";
                        productno1.attribute("stockcount").setValue(socketcount);
                    }
                }


            }else{
                System.out.println("库存不足");
            }
        }

        save1();

    }

    public static void main(String[] args) throws DocumentException, IOException {
        showInfo();
        Scanner in=new Scanner(System.in);
        System.out.println("请输入编号");
        String num=in.next();
        System.out.println("数量");
        int count=in.nextInt();
        buy(num,count);
    }
}
